package com.xy.p2p.action.admin;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.security.BadCredentialsException;
import com.xy.common.security.UsernameNotFoundException;
import com.xy.common.web.CookieUtils;
import com.xy.common.web.RequestUtils;
import com.xy.core.web.WebErrors;

import com.xy.p2p.model.Ttenderrecord;
import com.xy.p2p.service.TtenderrecordService;



@Controller
public class TtenderrecordAction{
	private static final Logger log = LoggerFactory.getLogger(TtenderrecordAction.class);
	public TtenderrecordAction() {
	}
 
 
 	/**
	 * 投标记录表新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_ttenderrecordAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("ttenderrecord", new Ttenderrecord());
        setDict(model);
		return "ttenderrecordEdit";
	}

	/**
	 * 投标记录表新增保存
	 * 
	 * @param ttenderrecord
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/o_ttenderrecordSave.do")
	public String save(Ttenderrecord ttenderrecord, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(ttenderrecord, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		ttenderrecord = ttenderrecordService.save(ttenderrecord);
		return "redirect:v_ttenderrecordList.do";
	}

	/**
	 * 投标记录表删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_ttenderrecordDelete.do")
	public String delete(long[] ids) {
		if (ids != null) {
			for (long id : ids) {
				ttenderrecordService.remove(id);
			}
		}
		return "redirect:v_ttenderrecordList.do";
	}

	/**
	 * 投标记录表编辑页面
	 * 
	 * @param trid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_ttenderrecordEdit.do")
	public String edit(long trid, HttpServletRequest request, ModelMap model) {
		Ttenderrecord ttenderrecord = ttenderrecordService.get(trid);
		model.addAttribute("ttenderrecord", ttenderrecord);
        setDict(model);
		return "ttenderrecordEdit";
	}

	/**
	 * 投标记录表详情
	 * 
	 * @param trid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_ttenderrecordDetail.do")
	public String detail(long trid, HttpServletRequest request, ModelMap model) {
		Ttenderrecord ttenderrecord = ttenderrecordService.get(trid);
		model.addAttribute("ttenderrecord", ttenderrecord);
        setDict(model);
		return "ttenderrecordView";
	}

	/**
	 * 投标记录表查询（分页）
	 * 
	 * @param ttenderrecord
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_ttenderrecordList.do")
	public String listPage(Ttenderrecord ttenderrecord, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = ttenderrecordService.getTtenderrecordByPage(toMap(ttenderrecord),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "ttenderrecordList";
	}

	/**
	 * 投标记录表查询
	 * 
	 * @param ttenderrecord
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_ttenderrecordList1.do")
	public String list(Ttenderrecord ttenderrecord, HttpServletRequest request, ModelMap model) {
		List<Ttenderrecord> list = ttenderrecordService.getTtenderrecord(toMap(ttenderrecord));
		model.addAttribute("list", list);
        setDict(model);
		return "ttenderrecordList";
	}


 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param ttenderrecord
	 * @return
	 */	private Map<String,Object> toMap(Ttenderrecord ttenderrecord){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(ttenderrecord.getTrid()!=null&&!ttenderrecord.getTrid().equals("")){
			map.put("trid", ttenderrecord.getTrid());
		}
		if(ttenderrecord.getTfinanceinfo()!=null&&!ttenderrecord.getTfinanceinfo().equals("")){
			map.put("Tfinanceinfo", ttenderrecord.getTfinanceinfo());
		}
		if(ttenderrecord.getTmember()!=null&&!ttenderrecord.getTmember().equals("")){
			map.put("Tmember", ttenderrecord.getTmember());
		}
		if(ttenderrecord.getTdmoney()!=null&&!ttenderrecord.getTdmoney().equals("")){
			map.put("tdmoney", ttenderrecord.getTdmoney());
		}
		if(ttenderrecord.getTddatetime()!=null&&!ttenderrecord.getTddatetime().equals("")){
			map.put("tddatetime", ttenderrecord.getTddatetime());
		}
		if(ttenderrecord.getTdreason()!=null&&!ttenderrecord.getTdreason().equals("")){
			map.put("tdreason", ttenderrecord.getTdreason());
		}
		if(ttenderrecord.getFinancepa()!=null&&!ttenderrecord.getFinancepa().equals("")){
			map.put("financepa", ttenderrecord.getFinancepa());
		}
		if(ttenderrecord.getMibmoney()!=null&&!ttenderrecord.getMibmoney().equals("")){
			map.put("mibmoney", ttenderrecord.getMibmoney());
		}
		if(ttenderrecord.getIstransfer()!=null&&!ttenderrecord.getIstransfer().equals("")){
			map.put("istransfer", ttenderrecord.getIstransfer());
		}
		if(ttenderrecord.getTsfmoney()!=null&&!ttenderrecord.getTsfmoney().equals("")){
			map.put("tsfmoney", ttenderrecord.getTsfmoney());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
      model.addAttribute("istransferMap", DictUtils.getDictByType(DictUtils.TTENDERRECORD_ISTRANSFER));		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Ttenderrecord ttenderrecord, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TtenderrecordService ttenderrecordService;   
}